import { _decorator, Component, Node, Prefab } from 'cc';
import { ModuleBase } from './ModuleBase';
import { Card } from './Card';
import { CardMgr } from './CardMgr';
const { ccclass, property } = _decorator;

@ccclass('Cards')
export class Cards extends ModuleBase {
    @property({type:Prefab, displayName:'Card'})
    Card:Prefab = null;
    private _handCard:Card[]=[];
    get handCard(){
        return this._handCard;
    }
    set handCard(card:Card[]){
        this._handCard=card;
    }
    //private _UpCards:Card[]=[];
    private maxupCard:number=5;
    private upCard:number=0;
    //private Cardnum:number=8;
    private SortBtn:Node
    onInit(...rest: any[]): void {
        //console.log(1);
        //注册出牌弃牌整理牌按钮


    }    
    sortCard(){
        this.SendMsg("SortCard","CardMgr",this._handCard);
    }
    playCard(){
        //将选中卡牌从手牌中移除并抽牌
        this.SendMsg("PlayCard","CardMgr",this._handCard);
    }
    foldCard(){
        //将选中卡牌从手牌中移除并抽牌
        this.SendMsg("foldCard","CardMgr",this._handCard);
    }
    onUpdate(dt: number, ...rest: any[]): void {
        //点击的手牌数不能超过5
        for(let i=0;i<=this._handCard.length-1;i++){
            //console.log(this._handCard[i]);
            if(this._handCard[i].istouch){
                //console.log(i);
                this.upCard+=1;
                //当有手牌被选中时激活按钮
                this.node.parent.getChildByName("_PlayCardBtn").active = true;
                this.node.parent.getChildByName("_DisCardBtn").active = true;          
            }
        }
        if (this.upCard == 0) {
            this.node.parent.getChildByName("_PlayCardBtn").active = false;
            this.node.parent.getChildByName("_DisCardBtn").active = false;
        }
        if (this.upCard === this.maxupCard) {
            for (let i = 0; i <= this._handCard.length - 1; i++) {
                this._handCard[i].isMax = true;
            }
            //return;
        }
        else{
            for (let i = 0; i <= this._handCard.length - 1; i++) {
                this._handCard[i].isMax = false;
            }
        }
        //归零开始下一次检测
        this.upCard = 0;
    }
}


